package cn.minsh.lims.eoms.modules.app.service.impl;

import cn.minsh.lims.eoms.common.utils.PageUtils;
import cn.minsh.lims.eoms.common.utils.Query;
import cn.minsh.lims.eoms.modules.app.dao.MaintenanceOrderRecordDao;
import cn.minsh.lims.eoms.modules.app.entity.MaintenanceOrderRecordEntity;
import cn.minsh.lims.eoms.modules.app.service.MaintenanceOrderRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("maintenanceOrderRecordService")
public class MaintenanceOrderRecordServiceImpl extends ServiceImpl<MaintenanceOrderRecordDao, MaintenanceOrderRecordEntity> implements MaintenanceOrderRecordService {

    @Autowired
    private MaintenanceOrderRecordDao orderRecordDao ;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MaintenanceOrderRecordEntity> page = this.page(
                new Query<MaintenanceOrderRecordEntity>().getPage(params),
                new QueryWrapper<MaintenanceOrderRecordEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<MaintenanceOrderRecordEntity> listByMaintenanceId(Long maintenanceId) {
        QueryWrapper<MaintenanceOrderRecordEntity> wrapper = new QueryWrapper<>() ;
        wrapper.eq("maintenance_order_id",maintenanceId) ;
        wrapper.orderByAsc("id") ;
        return orderRecordDao.selectList(wrapper);
    }

    @Override
    public List<MaintenanceOrderRecordEntity> listByMaintenanceId(Long operatorId, Long maintenanceId) {
        QueryWrapper<MaintenanceOrderRecordEntity> wrapper = new QueryWrapper<>() ;
        wrapper.eq("operator_id",operatorId) ;
        wrapper.eq("maintenance_order_id",maintenanceId) ;
        wrapper.orderByAsc("id") ;
        return orderRecordDao.selectList(wrapper);
    }

    @Override
    public List<MaintenanceOrderRecordEntity> listRepeatByUserId(Long userId) {
        QueryWrapper<MaintenanceOrderRecordEntity> wrapper = new QueryWrapper<>() ;
        wrapper.eq("operator_id",userId) ;
        wrapper.orderByDesc("id") ;
        List<MaintenanceOrderRecordEntity> list = orderRecordDao.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)){
            return list ;
        }
        List<String> repeatList = new ArrayList<>() ;
        List<MaintenanceOrderRecordEntity> retList = new ArrayList<>() ;
        list.stream().forEach(e->{
            if(repeatList.contains(e.getMaintenanceOrderId()+""+e.getOperatorId())){
                return;
            }
            repeatList.add(e.getMaintenanceOrderId()+""+e.getOperatorId()) ;
            retList.add(e) ;
        });
        return retList;
    }

}